﻿// Sequin.WebLogs.IO.LogWriter
//
// (c) 2011 Arthur Pitman
//
// Part of the Sequin Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License (Version 2.1) as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License (Version 2.1) along with this library; if not, write to the 
// Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Sequin.WebLogs.IO
{
    /// <summary>
    /// Writes a generic log file
    /// Format: timestamp request_type request response_code referrer ip_address user_agent
    /// </summary>
    public class LogWriter : ILogWriter
    {
        /// <summary>
        /// The <see cref="StreamWriter"/> associated with the instance
        /// </summary> 
        protected StreamWriter writer = null;

        /// <summary>
        /// Initializes a new <see cref="LogWriter"/> instance to write to the specified file
        /// </summary>
        /// <param name="filename">The file to be created and written to</param>
        public LogWriter(string filename)
        {            
            writer = File.CreateText(filename);
        }

        /// <summary>
        /// Writes a <see cref="LogEntry"/>
        /// </summary>
        /// <param name="entry">The <see cref="LogEntry"/> to write</param>
        public void WriteLogEntry(LogEntry entry)
        {
            writer.WriteLine(entry.ToString());
        }

        /// <summary>
        /// Closes the writer
        /// </summary>
        public void Close()
        {
            writer.Close();
            writer = null;
        }
    }
}
